﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace TTBox
{
    public partial class MsgPanel : UserControl
    {

        public enum MsgType { Dispatch, ReserveJob, Msg };


        public MsgType m_msgType = MsgType.Dispatch;
        public int m_nShowIndex = 0;
        public int m_nSelectedIndex = -1;
        ArrayList m_alMsg;
        public MsgPanel()
        {
            InitializeComponent();
        }

        public void initial(MsgType arg_mt)
        {
            m_nSelectedIndex = -1;
            m_nShowIndex = 0;
            this.button_F1.Visible = false;
            this.button_F2.Visible = true;
            m_msgType = arg_mt;
            this.button_S.Visible = true;

            switch (m_msgType)
            {
                case MsgType.Dispatch:

                    break;
                case MsgType.Msg:

                    break;
                case MsgType.ReserveJob:
                    this.button_S.Visible = false;
                    break;
            }

            m_alMsg = getMsgList();
            showMsgLines();
            

        }
        public void initialButton()
        {
            this.button_F1.Text = "上頁";
            this.button_F2.Text = "下頁";
            this.button_F3.Text = "返回";

            if (m_msgType != MsgType.ReserveJob)
            {
                this.button_S.Visible = true;
                this.button_S.Text = "全刪";
            }
            else
            {
                this.button_S.Visible = false;
            }
        }

        public ArrayList getMsgList()
        {
            switch (m_msgType)
            {
                case MsgType.Dispatch:
                    return StoreData.getInstance().getDispatch();
                case MsgType.Msg:
                    return StoreData.getInstance().getMsg();
                case MsgType.ReserveJob:
                    return StoreData.getInstance().getReserveJob();
            }
            return new ArrayList();
        }

        public void delMsg(int arg_nIndex)
        {

            switch (m_msgType)
            {
                case MsgType.Dispatch:
                     StoreData.getInstance().delDispatch(arg_nIndex);
                     m_alMsg = getMsgList();

                     break;
                case MsgType.Msg:
                    StoreData.getInstance().delMsg(arg_nIndex);
                    m_alMsg = getMsgList();

                    break;
                case MsgType.ReserveJob:
                    StoreData.getInstance().delReserveJob(arg_nIndex);
                    m_alMsg = getMsgList();
                    break;
            }


            showMsgLines();
        }

        public string getHeaderText(string arg_strOri, int arg_nTitileStart, int arg_nTitleLen, int arg_nTextStart, int arg_nTextLen)
        {
            try
            {
                return arg_strOri.Substring(arg_nTitileStart, arg_nTitleLen);
            }
            catch (Exception ex)
            {
                return "XXXXXXXX";
            }
        }

        public string getBodyText(string arg_strOri, int arg_nTitileStart, int arg_nTitleLen, int arg_nTextStart, int arg_nTextLen)
        {
            try
            {
                return arg_strOri.Substring(arg_nTextStart);// arg_strOri.Substring(arg_nTextStart, nLastLen);
            }
            catch (Exception ex)
            {
                return "錯誤:"+ex.Message;
            }
        }

        private void showMsgLines()
        {

            int nHeaderLen = 2;
            int nTitleStart = 5;
            int nTitleLen = 8;
            int nTextStart = 15;
            int nTextLen = 24;

            switch (m_msgType)
            {
                case MsgType.Dispatch:
                    nTitleStart = 6;
                    nTextStart = 19;
                    nTextLen = 24;

                    break;
                case MsgType.ReserveJob:
                    nTitleStart = 6;
                    nTextStart = 19;
                    nTextLen = 24;

                    break;
            }



            if (m_nShowIndex < m_alMsg.Count)
            {
                string strTemp = ((string)m_alMsg[m_nShowIndex]).Substring(nHeaderLen);
                this.button_Line1.Text = getHeaderText(strTemp, nTitleStart, nTitleLen, nTextStart, nTextLen); // strTemp.Substring(nTitleStart, nTitleLen) + " " + strTemp.Substring(nTextStart, nTextLen);
                this.label_Line1.Text = getBodyText(strTemp, nTitleStart, nTitleLen, nTextStart, nTextLen); // strTemp.Substring(nTitleStart, nTitleLen) + " " + strTemp.Substring(nTextStart, nTextLen);
                this.button_Line1.Tag = m_nShowIndex;
                this.button_Line1.Visible = true;

            }
            else
            {
                this.button_Line1.Text = "";
                this.button_Line1.Tag = null;
                this.button_Line1.Visible = false;
            }
            if (m_nShowIndex + 1 < m_alMsg.Count)
            {
                string strTemp = ((string)m_alMsg[m_nShowIndex+1]).Substring(nHeaderLen);
                this.button_Line2.Text = getHeaderText(strTemp, nTitleStart, nTitleLen, nTextStart, nTextLen); //this.button_Line2.Text = strTemp.Substring(nTitleStart, nTitleLen) + " " + strTemp.Substring(nTextStart, nTextLen);
                this.label_Line2.Text = getBodyText(strTemp, nTitleStart, nTitleLen, nTextStart, nTextLen); //this.button_Line2.Text = strTemp.Substring(nTitleStart, nTitleLen) + " " + strTemp.Substring(nTextStart, nTextLen);
                this.button_Line2.Tag = m_nShowIndex + 1;
                this.button_Line2.Visible = true;
            }
            else
            {
                this.button_Line2.Text = "";
                this.button_Line2.Tag = null;
                this.button_Line2.Visible = false;
            }
            if (m_nShowIndex + 2 < m_alMsg.Count)
            {
                string strTemp = ((string)m_alMsg[m_nShowIndex+2]).Substring(nHeaderLen);
                this.button_Line3.Text = getHeaderText(strTemp, nTitleStart, nTitleLen, nTextStart, nTextLen); //this.button_Line3.Text = strTemp.Substring(nTitleStart, nTitleLen) + " " + strTemp.Substring(nTextStart, nTextLen);
                this.label_Line3.Text = getBodyText(strTemp, nTitleStart, nTitleLen, nTextStart, nTextLen); //this.button_Line3.Text = strTemp.Substring(nTitleStart, nTitleLen) + " " + strTemp.Substring(nTextStart, nTextLen);
                this.button_Line3.Tag = m_nShowIndex + 2;
                this.button_Line3.Visible = true;

            }
            else
            {
                this.button_Line3.Text = "";
                this.button_Line3.Tag = null;
                this.button_Line3.Visible = false;
            }
            if (m_nShowIndex + 3 < m_alMsg.Count)
            {
                string strTemp = ((string)m_alMsg[m_nShowIndex+3]).Substring(nHeaderLen);
                this.button_Line4.Text = getHeaderText(strTemp, nTitleStart, nTitleLen, nTextStart, nTextLen); //this.button_Line4.Text = strTemp.Substring(nTitleStart, nTitleLen) + " " + strTemp.Substring(nTextStart, nTextLen);
                this.label_Line4.Text = getBodyText(strTemp, nTitleStart, nTitleLen, nTextStart, nTextLen); //this.button_Line4.Text = strTemp.Substring(nTitleStart, nTitleLen) + " " + strTemp.Substring(nTextStart, nTextLen);
                this.button_Line4.Tag = m_nShowIndex + 3;
                this.button_Line4.Visible = true;

            }
            else
            {
                this.button_Line4.Text = "";
                this.button_Line4.Tag = null;
                this.button_Line4.Visible = false;

            }

            this.label_Line1.Visible = this.button_Line1.Visible;
            this.label_Line2.Visible = this.button_Line2.Visible;
            this.label_Line3.Visible = this.button_Line3.Visible;
            this.label_Line4.Visible = this.button_Line4.Visible;

            if (m_nShowIndex <= 0)
            {
                m_nShowIndex = 0;
                this.button_F1.Visible = false;
            }
            if (m_nShowIndex + 4 >= m_alMsg.Count)
                this.button_F2.Visible = false;


        }


        private void button_F1_Click(object sender, EventArgs e)
        {
            m_nShowIndex -= 4;
            this.button_F1.Visible = true;
            this.button_F2.Visible = true;

            if (m_nShowIndex <= 0)
            {
                m_nShowIndex = 0;
                this.button_F1.Visible = false;
            }
            showMsgLines();
        }

        private void button_F2_Click(object sender, EventArgs e)
        {
            m_nShowIndex += 4;

            this.button_F1.Visible = true;
            this.button_F2.Visible = true;
            if (m_nShowIndex + 4 >= m_alMsg.Count)
                this.button_F2.Visible = false;

            showMsgLines();

        }


        private void button_F3_Click(object sender, EventArgs e)
        {
               Form_Main.Main.ChangeToMainScreen();
        }

        private void button_F4_Click(object sender, EventArgs e)
        {

            
        }

        public void showMsgContent(int arg_nIndex)
        {
            m_nSelectedIndex = arg_nIndex;
            try
            {
                string strTemp = (string)m_alMsg[m_nSelectedIndex];
                switch (m_msgType)
                {
                    case MsgType.Dispatch:
                        Form_Main.Main.ChangeToJobRead(strTemp.Substring(5), m_nSelectedIndex);

                        break;
                    case MsgType.Msg:

                        Form_Main.Main.ChangeToMessageRead(strTemp.Substring(2), m_nSelectedIndex);
                        break;
                    case MsgType.ReserveJob:
                        Form_Main.Main.ChangeToReserveJobMessageRead(strTemp.Substring(2));

                        break;
                }
            }
            catch (Exception ex)
            {
                LogManager.getInstance().Log("error , no message..");
            }
        }

        private void button_Line_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (b.Tag != null)
            {
                int nIndex = (int)b.Tag;

                showMsgContent(nIndex);

                
            }

        }

        private void button_F8_Click(object sender, EventArgs e)
        {
        }

        private void button_S_Click(object sender, EventArgs e)
        {
            if (m_msgType != MsgType.ReserveJob)
            {
                if (button_Line4.Visible) delMsg((int)button_Line4.Tag);
                if (button_Line3.Visible) delMsg((int)button_Line3.Tag);
                if (button_Line2.Visible) delMsg((int)button_Line2.Tag);
                if (button_Line1.Visible) delMsg((int)button_Line1.Tag);
            }
            initialButton();

        }

    }
}
